<template>
  <el-dialog v-model="dialogShow" :title="dialog.title" width="800px" @close="handleCloseDialog()">
    <div v-loading="dialog.loading">
      <el-descriptions>
        <el-descriptions-item label="任务名称">{{ formData?.name }}</el-descriptions-item>
        <el-descriptions-item label="角色">
          {{ formData && FLOW_ACT_ROLE_LABEL[formData.activityRole] }}
        </el-descriptions-item>
        <el-descriptions-item label="候选类型">
          {{ formData && FLOW_CANDIDAT_TYPE_LABEL[formData.candidateType] }}
        </el-descriptions-item>
        <el-descriptions-item label="候选人">{{ formData?.candidateName }}</el-descriptions-item>
        <el-descriptions-item label="任务描述">{{ formData?.remark }}</el-descriptions-item>
      </el-descriptions>

      <!-- 属性列表 -->
      <el-table
        :data="formData?.dataConfigs"
        stripe
        size="small"
        height="300"
        class="data-table__content"
      >
        <el-table-column type="index" width="60" align="center" />
        <el-table-column label="填报项名称" prop="property"></el-table-column>
        <el-table-column label="数据类型" prop="dataType">
          <template #default="scope">
            <el-tag>
              {{ FLOW_TASK_DATA_TYPE_LABEL[scope.row.dataType] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="数据备选项" prop="options"></el-table-column>
      </el-table>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import ActivityNodeTaskAPI, { ActivityNodeTaskDetail } from "@/api/flow/activityNodeTask.api";
import {
  FLOW_ACT_ROLE_LABEL,
  FLOW_CANDIDAT_TYPE_LABEL,
  FLOW_TASK_DATA_TYPE_LABEL,
} from "@/api/flow/flowNodeTask.api";

const props = defineProps<{
  visible: boolean;
  id?: string;
}>();
const emits = defineEmits<{
  (e: "dialogClose", isRefresh?: boolean): void;
}>();

const dialog = reactive({
  title: "任务详情",
  loading: false,
});
// 处理props的数据 重新定义 dialogVisible
const dialogShow = computed({
  get() {
    return props.visible;
  },
  set(val) {
    return val;
  },
});

const formData = ref<ActivityNodeTaskDetail>();

watch(
  () => props.visible,
  (val) => {
    if (val) {
      loadFlowDetail();
    }
  },
  {
    immediate: true,
  }
);

/**
 * 加载详情
 */
const loadFlowDetail = () => {
  if (!props.id) {
    return;
  }
  dialog.loading = true;
  formData.value = undefined;
  ActivityNodeTaskAPI.loadDetail(props.id)
    .then((data) => {
      formData.value = data;
    })
    .finally(() => {
      dialog.loading = false;
    });
};

/**
 * 关闭弹窗
 */
function handleCloseDialog(isRefresh?: boolean) {
  emits("dialogClose", isRefresh);
}
</script>

<style lang="scss" scoped></style>
